package serializer

import (
	"singo/model"
	"strings"
	"time"
)

// TotalStuct 序列化器
type TotalStuct struct {
	Total int `json:"total"`
	Done  int `json:"done"`
}

// TotalStuct2 序列化器
type TotalStuct2 struct {
	Total float64 `json:"total"`
	Done  float64 `json:"done"`
}

type ProjectManage struct {
	UID     string      `json:"id"`
	Name    string      `json:"name"`
	Head    interface{} `json:"head"`
	Task    TotalStuct  `json:"task"`
	Demand  TotalStuct  `json:"demand"`
	Defect  TotalStuct  `json:"defect"`
	Overdue int         `json:"overdue"`
}

type MemberManage struct {
	Nickname string      `json:"nickname"`
	Task     TotalStuct  `json:"task"`
	Weight   TotalStuct2 `json:"weight"`
	Demand   TotalStuct  `json:"demand"`
	Defect   TotalStuct  `json:"defect"`
	Overdue  int         `json:"overdue"`
	Check    TotalStuct  `json:"check"`
}

type ManageSummary struct {
	UID        string        `json:"id"`
	Name       string        `json:"name"`
	Intro      string        `json:"intro"`
	Status     uint          `json:"status"`
	Priority   uint          `json:"priority"`
	ExecuteBy  []interface{} `json:"execute_by"`
	Project    interface{}   `json:"project"`
	Progress   int           `json:"progress"`
	Weight     float64       `json:"weight"`
	StartAt    string        `json:"start_at"`
	DeadlineAt string        `json:"deadline_at"`
	CreatedAt  string        `json:"created_at"`
	UpdatedAt  string        `json:"updated_at"`
	CompleteAt string        `json:"complete_at"`
}

func BuilTotalSimple(item model.TotalInterface) TotalStuct {
	return TotalStuct{
		Total: item.Total,
		Done:  item.Done,
	}
}

func BuilTotalSimple2(item model.TotalInterface2) TotalStuct2 {
	return TotalStuct2{
		Total: item.Total,
		Done:  item.Done,
	}
}

func BuildSummaryManage(item model.ManageSummary) ManageSummary {
	var executeUsers []interface{}
	if item.ExecuteBy != "" {
		executeArr := strings.Split(item.ExecuteBy, ",")
		for _, item := range executeArr {
			executeUsers = append(executeUsers, BuildUserSimple(item))
		}
	} else {
		executeUsers = []interface{}{}
	}
	return ManageSummary{
		UID:        item.UID,
		Name:       item.Name,
		Intro:      item.Intro,
		Status:     item.Status,
		Priority:   item.Priority,
		Progress:   item.Progress,
		ExecuteBy:  executeUsers,
		Project:    BuildProjectSimple(item.ProjectID),
		Weight:     item.Weight,
		CreatedAt:  item.CreatedAt.In(time.Local).Format("2006-01-02 15:04:05"),
		UpdatedAt:  item.UpdatedAt.In(time.Local).Format("2006-01-02 15:04:05"),
		StartAt:    serializerTime(item.StartAt),
		DeadlineAt: serializerTime(item.DeadlineAt),
		CompleteAt: serializerTime(item.CompleteAt),
	}
}

func BuildProjectManage(item model.ManageProject) ProjectManage {
	return ProjectManage{
		UID:     item.UID,
		Name:    item.Name,
		Head:    BuildUserSimple(item.Head),
		Task:    BuilTotalSimple(item.Task),
		Demand:  BuilTotalSimple(item.Demand),
		Defect:  BuilTotalSimple(item.Defect),
		Overdue: item.Overdue,
	}
}

// BuildProjectsManage 序列化列表
func BuildProjectsManage(items []model.ManageProject) (projects []ProjectManage) {
	for _, item := range items {
		project := BuildProjectManage(item)
		projects = append(projects, project)
	}
	return projects
}

func BuildMemberManage(item model.ManageMember) MemberManage {
	return MemberManage{
		Nickname: item.Nickname,
		Weight:   BuilTotalSimple2(item.Weight),
		Task:     BuilTotalSimple(item.Task),
		Demand:   BuilTotalSimple(item.Demand),
		Defect:   BuilTotalSimple(item.Defect),
		Overdue:  item.Overdue,
		Check:    BuilTotalSimple(item.Check),
	}
}

// BuildMembersManage 序列化列表
func BuildMembersManage(items []model.ManageMember) (projects []MemberManage) {
	for _, item := range items {
		project := BuildMemberManage(item)
		projects = append(projects, project)
	}
	return projects
}

// BuildSummarysManage 序列化列表
func BuildSummarysManage(items []model.ManageSummary) (summarys []ManageSummary) {
	for _, item := range items {
		summary := BuildSummaryManage(item)
		summarys = append(summarys, summary)
	}
	return summarys
}
